#!/bin/sh
# javaif - Java Control Panel intermediary in case Java not installed
# Invoked by the Java Control Panel menu untry.
# When Java is completely installed, environmental variabble JAVAHOME and JAVAJRE point to it.  In addition, the PATH includes the Java directory.  If Java is not fully installed, this script assists in completing a JDK/JRE installation, once a java PET or SFS package is installed.

JAVAHOME="$(grep 'JAVAHOME=' /etc/javaif.conf 2>/dev/null | cut -f 2 -d =)"
JAVANEWEST="$(javaiffind)"
[ -n "$JAVAHOME" ] \
 && [ "$JAVAHOME" = "$JAVANEWEST" ] \
 && [ -x $JAVAHOME/bin/jcontrol ] \
 && [ "$JAVA_HOME" = "$JAVAHOME" ] \
 && exec jcontrol "$@"

export TEXTDOMAIN=javaif
export OUTPUT_CHARSET=UTF-8
TITLE="Javaif - $(gettext 'Java Installation')"

if [ -z "$JAVAHOME" -a -z "$JAVANEWEST" ]; then
 Xdialog --wmclass "java" --title "$TITLE" --icon /usr/local/lib/X11/pixmaps/error.xpm --stdout --backtitle "\n$(gettext 'The Java Runtime Environment is not installed.')\n" --left --wrap --msgbox "$(gettext 'You can install a Java JRE or JDK SFS or PET package, or expand a "tarball" archive package from Oracle (TM) into directory /OPT.')\n\n$(gettext 'To obtain the latest version of Java, you can use get_java (from menu>Utility>Get Java Runtime/Development)')\n\n$(gettext 'Ensure that the installation is complete by re-running the Java Control Panel')\n" 0 80
else
 if [ -n "$JAVANEWEST" ]; then
  MSG1="$(gettext 'A new Java Runtime Environment is partially installed.')"
  . gettext.sh
  VERSIONNEWEST="$($JAVANEWEST/bin/java -version 2>&1 | grep 'version' | cut -f 2 -d \")"
  MSG2=$(eval_gettext "To complete the installation of Java version \$VERSIONNEWEST, please restart (reboot) the system.")
  MSG3="$(gettext 'Until you reboot, Java cannot be used.')"
 else
  MSG1="$(gettext 'The Java Runtime Environment is only partially uninstalled.')"
  MSG2=$(gettext 'To complete the uninstallation of Java, please restart (reboot) the system.')
  MSG3="$(gettext 'Java can no longer be used.')"
 fi
 
 if [ -n "$JAVA_HOME" ] && [ -x $JAVA_HOME/bin/jcontrol ]; then
  Xdialog --wmclass "java" --title "$TITLE" --icon /usr/local/lib/X11/pixmaps/info.xpm --stdout --backtitle "\n$MSG1\n" --left --wrap --yesno "$MSG2\n\n$(gettext 'Until you reboot the system, the old version of Java remains usable.')\n\n$(gettext 'Continue to the Java Control Panel?')\n" 0 80
  [ $? -eq 0 ] \
   && [ -d $JAVAHOME ] \
   && exec jcontrol "$@"
 else
  Xdialog --wmclass "java" --title "$TITLE" --icon /usr/local/lib/X11/pixmaps/info.xpm --stdout --backtitle "\n$MSG1\n" --left --wrap --msgbox "$MSG2\n\n$MSG3\n" 0 80
 fi
fi
exit 1
